﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Shiny.SmartTravel.Data.Local
{
    /// <summary>
    /// For extended method usage.
    /// </summary>
    public static class ForExtensions
    {
        /// <summary>
        /// Get the map file path for province
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetLocalMapFile(this Province p)
        {
            string[] domains = p.Domain.Split('.');

            return string.Format("{0}\\{1}.jpg", Settings.LocalMapsFolder, domains[0]);
        }

        /// <summary>
        /// Get the sub Sights if any sight has
        /// </summary>
        /// <param name="sight"></param>
        /// <returns></returns>
        public static IEnumerable<Sight> GetSubSights(this Sight sight)
        {
            if (sight.IsSmall)
                return null;

            string path = Settings.LocalDataFolder + "\\Sights.xml";
            XDocument xdoc = XDocument.Load(path);
            IEnumerable<Sight> sights = from s in xdoc.Element("XutourSights").Elements("Sight")
                                        where (int)s.Element("ParentSightId") == sight.ParentSightId
                                        select new Sight { Id = (int)s.Element("Id"), ProvinceId = (int)s.Element("ProvinceId"), CityId = (int)s.Element("CityId"), ParentSightId = (int)s.Element("ParentSightId"), IsSmall = (bool)s.Element("IsSmall"), Name = (string)s.Element("Name"), Image = (string)s.Element("Image"), Intro = (string)s.Element("Intro"), UriName = (string)s.Element("UriName"), Domain = (string)s.Element("Domain") };
            return sights;
        }
    }
}
